package com.edward.service;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.edward.PagerModel;
import com.edward.model.OtherHarvest;

public class OtherHarvestManager extends HibernateDaoSupport {
	
	public void addOtherHarvest(OtherHarvest otherHarvest) {
		
		getHibernateTemplate().save(otherHarvest);
	}
	
	public void updateOtherHarvest(OtherHarvest otherHarvest) {
		
		getHibernateTemplate().update(otherHarvest);
	}
	
	public OtherHarvest getOtherHarvest(int id){
		OtherHarvest d =(OtherHarvest)getHibernateTemplate().get(OtherHarvest.class, id);
		return d;
	}
	

	@SuppressWarnings("unchecked")
	public List<OtherHarvest> listOtherHarvest() {
		return getHibernateTemplate().find("from OtherHarvest");
	}

	@SuppressWarnings("unchecked")	
	public String addOther (Date date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String d = sdf.format(date);
		List <OtherHarvest> OtherList = getHibernateTemplate().find("from OtherHarvest s where s.type is null and s.date = '" + d + "'");
		if(OtherList.size()>0){
			return "以存在对应数据，添加失败!";
		}
		
			OtherHarvest other = new OtherHarvest();
			List <OtherHarvest> o = new ArrayList<OtherHarvest>();
			other.setDate(date);
			other.setOthers(o);
			addOtherHarvest(other);
			for(int i=0; i<2;i++){
					OtherHarvest oh = new OtherHarvest();
					oh.setType(judge(i));
					oh.setDate(date);
					addOtherHarvest(oh);
					other.getOthers().add(oh);
			}
			updateOtherHarvest(other);
			return "添加成功"; 
		}
	
	@SuppressWarnings("unchecked")	
	public void updateOther (OtherHarvest o){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String d = sdf.format(o.getDate());
		List <OtherHarvest>l = getHibernateTemplate().find("from OtherHarvest s where s.type is null and s.date = '" + d + "'");
		OtherHarvest oh = l.get(0);
		OtherHarvest ot = oh.getOthers().get(judge(o.getType())) ;
		oh.setCost(oh.getCost() + o.getCost() - ot.getCost() );
		oh.setIncome(oh.getIncome() + o.getIncome() - ot.getIncome() );
		ot.setCost(o.getCost());
		ot.setIncome(o.getIncome());
		updateOtherHarvest(oh);
		updateOtherHarvest(ot);
	}
		
	
	

	
	@SuppressWarnings("unchecked")	
	public PagerModel pagerList(int start,int limit,String query) {
		if(query.equals("")){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
			query = sdf.format(new Date());
		}
		List <OtherHarvest>l = getHibernateTemplate().find("from OtherHarvest s where s.type is null and year(s.date) = '" + query + "' order by s.date");
		List <OtherHarvest> list = new ArrayList<OtherHarvest>();
		OtherHarvest oh = new OtherHarvest();
		for(OtherHarvest other : l ){
			for(OtherHarvest a :other.getOthers()){
				list.add(a);
			}
			oh.setCost(oh.getCost() + other.getCost() );
			oh.setIncome(oh.getIncome() + other.getIncome());
			oh.setDate(null);
			other.setOthers(null);
			list.add(other);
		}
		list.add(oh);
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <OtherHarvest>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
	}
	
	public int judge (String s ){
		if(s.equals("其他1")){
			return 0;
		}else{
			return 1;
		}
	}
	
	public String judge (int i){
		if(i == 0){
			return "其他1";
		}{
			return "其他2";
		}
	}
	


}



